package com.psfd.demo.controller;

import com.psfd.demo.bean.Product;
import com.psfd.demo.service.ProductService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
public class ProductController {
    @Resource
    private ProductService productService;

    @RequestMapping("/proinsert")
    public ModelAndView insert(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("proadd");
        return modelAndView;
    }


    @RequestMapping("/insertPro")
    public ModelAndView add(Product product){
        System.err.println(product+"1111111111111");
        productService.save(product);
        return queryAll();
    }




    @RequestMapping("/proupdate")
    public ModelAndView proupdate(Integer proid){
        System.out.println(proid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("proid",proid);
        modelAndView.setViewName("proupdate");
        return modelAndView;
    }

    @RequestMapping("/updatePro")
    public ModelAndView updatePro(Product product){
        System.err.println(product+"--------------------");
        productService.updateById(product);
        return queryAll();
    }

    @RequestMapping("/deletePro")
    public ModelAndView delete(Integer proid){
        System.err.println(proid+"....................");
        productService.removeById(proid);
        return queryAll();
    }


    @RequestMapping("/proQueryAll")
    public ModelAndView queryAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("prolist",productService.list());
        modelAndView.setViewName("proList");
        return modelAndView;
    }


//    @RequestMapping("/proreturnpage")
//    public ModelAndView returnpage(){
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("homepage");
//        return modelAndView;
//    }
}
